from bottle import route, run, static_file, request, BaseRequest, template


# 127.0.0.1:8001/
@route('/')
def index():
    return static_file("index.html", root='templates/')

# 这里区别就来了, Django默认转义, 而Bottle默认不转义
@route('/show_raw_html')
def index():
    return '<h1>Hahahah</h1>'

# show html page
@route('/form')
def show_form():
    return static_file('form.html', root='templates/')

# Get
@route('/gettest')
def get_test():
    id = request.GET.get('id')
    return id

# Post
@route('/mypost', method='POST')
def post_test():
    bootitle = request.forms.getunicode('booktitle')
    return bootitle

# 模板
@route('/temp')
def get_test():
    myvalue = 'shuoGG'
    username = 'Xiaoming'
    return template('template', myvalue=myvalue, username=username)

# Json
@route('/ajax')
def ajax_test() :
    return {'myvalue':'ajaxOK'}


# 127.0.0.1:8001/img.jpg
# 127.0.0.1:8001/123.txt
@route('/<filename:path>')
def show_staticfile(filename) :
    return static_file(filename, root='static_files/')




run(host="127.0.0.1", port=8001)